package leetcode.linked;

/**
 * @author: Oliver
 * @time: 2022/3/18/17:02
 */

class Node {
    int val;
    Node next;

    public Node(int val, Node next) {
        this.val = val;
        this.next = next;
    }
}

public class Linked {
    public static Node reverse(Node node) {
        Node pre = null;
        Node cur = node;
        while (cur != null) {
            Node next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }

    public static void print(Node node) {
        Node cur = node;
        while (cur != null) {
            System.out.println(cur.val);
            cur = cur.next;
        }
    }


    public static void main(String[] args) {
        Node node = new Node(2, new Node(1, new Node(5, null)));
        print(reverse(node));
    }

}

